// pages/Goods/Address/addAddress/index.js
const common = require("../../../../public.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        storeId:"",
        userInfo: '',
        token: '',
        isFromEdite: 1, //是否来自编辑页面 
        receiverState: '', //省
        receiverCity: '', //市
        receiverDistrict: '', //区
        receiverName: '', //收件人
        receiverMobile: '', //手机号
        address: '', // 省市区
        latitude: '',
        longitude: '',
        addressDet: '', // 详细地址
        isDefault: false, // 设为默认
        itemId: '', // 地址id
        region: [],

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (opt) {
        console.log('----传递--',opt);
        const that = this
        // 从编辑页面来带参数
        if (opt.from == 1) {
            console.log('opt', opt)
            wx.setNavigationBarTitle({
                title: '修改地址'
            })
            let {
                receiverName,
                receiverMobile,
                isDefault,
                receiverAddress,
                xpoint,
                ypoint,
            } = JSON.parse(opt.item)
            let addressList = null
            let regionArea = null
            let region = []
            let addressDet = null
            // 格式化地址
            try {
                addressList = receiverAddress ? receiverAddress.split(' ') : null
                regionArea = receiverAddress ? addressList[0] : null
                region = regionArea ? regionArea.split('-') : []
                addressDet = receiverAddress ? receiverAddress.slice(regionArea.length + 1) : null // regionArea后面紧跟空格，这里需要+1
            } catch (e) {
                console.log(e)
            } finally { }

            that.setData({
                addressDet,
                address: regionArea,
                region,
                storeId:opt.storeId,
                receiverName,
                receiverMobile,
                longitude: xpoint,
                latitude: ypoint,
                isDefault: isDefault === 1 ? true : false,
                itemId: JSON.parse(opt.item).id,
                isFromEdite: opt.from
            })
        } else { //从新增页面来不带参数
            wx.setNavigationBarTitle({
                title: '添加地址'
            })
            that.setData({
                storeId:opt.storeId,
                isFromEdite: 0,
                address: opt.address,
            })
        }

        common.getLocalUserInfo(function (success, data) {
            if (success) {
                that.setData({
                    token: data.token,
                    userInfo: data.user
                })
            }
        })
    },
    defaultChange({ detail }) { // 设为默认按钮
        this.setData({
            isDefault: detail ? true : false
        });
    },
    saveItem() { // 保存地址
        let that = this
        let Url
        if (common.isEmptyString(that.data.receiverName)) {
            common.showToast('请输入收件人')
            return
        }
        if (common.isEmptyString(that.data.receiverMobile)) {
            common.showToast('请输入联系方式')
            return
        }
        if (common.isEmptyString(that.data.address)) {
            common.showToast('请选择地址')
            return
        }
        if (common.isEmptyString(that.data.addressDet)) {
            common.showToast('请输入详细地址')
            return
        }
        let data = {
            token: that.data.token,
            userId: that.data.userInfo.id,
            receiverName: that.data.receiverName,
            xpoint:that.data.longitude,
            ypoint: that.data.latitude,
            receiverMobile: that.data.receiverMobile,
            receiverState: that.data.receiverState,
            receiverCity: that.data.receiverCity,
            receiverDistrict: that.data.receiverDistrict,
            receiverAddress: that.data.address + ' ' + that.data.addressDet,
            isDefault: that.data.isDefault === true ? 1 : 0,
        }
        if (that.data.isFromEdite == 1) { //从编辑页面来 调用更新接口
            Url = '/user/ship/update'
            data.id = that.data.itemId
        } else { // 调用新增接口
            Url = '/user/ship/add'
        }
        wx.showLoading({
            title: '加载中',
            icon: 'loading',
            mask: true
        })
console.log('参数为-0--',data,'---接口我',Url);
        // return;
        common.post(Url, data, function (res) {
            if (res.status == '200') {
                const pages = getCurrentPages();
              
                const prevPage = pages[pages.length - 2]; //上一个页面
                wx.navigateBack({
                    delta: 1,
                    success: () => {
                        common.showToast('保存成功')
                        console.log(prevPage,"444444")
                        if (prevPage && prevPage.getList) {
                            prevPage.paramArr=[]
                              prevPage.getList() //刷新方法
                        }
                    }
                })
            } else {
                common.showRequestErrMsg(res)
            }
        }, function () {
            wx.hideLoading({
                success: (res) => { },
            })
        })
    },
    deleteItem() { //删除地址
        const that = this
        common.showAlertView('删除', '是否要删除该地址？', function (result) {
            if (result) {
                let tempJson = {
                    id: that.data.itemId,
                    token: that.data.token,
                    isDefault: that.data.isDefault === true ? 1 : 0,
                    userId: that.data.userInfo.id
                }
                common.getJson('/user/ship/delete', tempJson, function (res) {
                    if (res.status === 200) {
                        const pages = getCurrentPages()
                        const prevPage = pages[pages.length - 2] //上一个页面
                        wx.navigateBack({
                            delta: 1,
                            success: () => {
                                common.showToast('删除成功')
                                if (prevPage && prevPage.getList) {
                                    prevPage.data.paramArr = []
                                
                                    prevPage.getList() //刷新方法
                                }
                            }
                        })
                        
                    } else {
                        common.showRequestErrMsg(res)
                    }
                })
            }
        })
    },
    bindRegionChange(e) {
        console.log("kkkkkkhhhhhhh")
        wx.navigateTo({
            url: '/packageC/pages/addAddressTencentMap/addAddressTencentMap?storeId=' + this.data.storeId,
        })
        //     console.log(e)
        //     const codeList = e.detail.code
        //     this.setData({
        //       receiverState: codeList[0],
        //       receiverCity: codeList[1],
        //       receiverDistrict: codeList[2]
        //     })
        //     const valueList = e.detail.value
        //     let valueName = []
        //     valueList.map(i => {
        //       valueName.push(i)
        //     })
        //     this.setData({
        //       address: valueName.join('-'),
        //     })
    }
})